// pages/Users/Suggest/Suggest.js
const app = getApp()
import { checkFile } from '../../../utils/util';
import { addUserSuggest } from '../../../api/services';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    uploadFileUrl: app.globalData.global.uploadFileUrl,
    brandId: app.globalData.global.brandId,
    content: '', // 反馈内容
    fileList: [],
    autosize: {
      'minHeight': '270rpx',
      'maxHeight': '270rpx'
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    let userId = wx.getStorageSync('userId') ?? 41;
    return {
      path: '/pages/Welcome/Welcome?scene='+userId,
    }
  },
  afterRead(event) {
    const { file } = event.detail;
    let _this = this;
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    var token = wx.getStorageSync('access_token');
    wx.showLoading({
    })
    wx.uploadFile({
      url: _this.data.uploadFileUrl, // 上传接口
      filePath: file.url,
      name: 'file',
      formData: { 'brandId': _this.data.brandId ,'access_token': token,},
      success(res) {
        console.log(res,'res')
        wx.hideLoading();
        var res = JSON.parse(res.data);
        if(res.success){
          // _this.setData({ imageUrl: res.data });
          const { fileList = [] } = _this.data;
          // fileList.push({ ...file, url: res.data });
          var types = checkFile(res.data);
          if(types == 'video'){
            fileList.push({url: res.data,isVideo: true,deletable: true})
          }else if(types == 'image'){
            fileList.push({url: res.data,isImage: true,deletable: true})
          }
          console.log(fileList,'fileList')
          _this.setData({ fileList });
        }else{
          wx.showToast({
            title: res.errMessage,
            icon: 'none',
          })
        }
      },
    });
  },
  // 删除照片
  delFileList(event){
    console.log(event.detail,'evemt')
    const {fileList} = this.data;
    fileList.splice(event.detail.index,1);
    this.setData({fileList})
  },
  // 输入框
  onChange(event) {
    // event.detail 为当前输入的值
    console.log(event.detail);
    const name = event.target.dataset.name||event.currentTarget.dataset.name;
    console.log(name,'name')
    var data = this.data;
    data[name] =  event.detail;
    this.setData(data);
  },
  // 确认反馈
  submit(){
    let _this = this;
    const { content , fileList } = _this.data;
    var imgUrlArr = [];
    for (let index = 0; index < fileList.length; index++) {
      const item = fileList[index];
      imgUrlArr.push(item.url);
    }
    addUserSuggest({content: content, imgUrl: imgUrlArr.length > 0 ? imgUrlArr.join(',') : '',})
    .then(res => {
      if(res.success){
        wx.showToast({
          title: '提交成功',
          icon: 'none',
        });
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
  handleAgreePrivacyAuthorizationFun(){
    console.log('同意之后的回调');
  },
  disagreePrivacyAuthorization(){
    this.resolvePrivacyAuthorization({ event: 'disagree' });
  },
})